﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;

namespace SPCIT.SP2010.Portal.DeleteMembershipUser
{
    [ToolboxItemAttribute(false)]
    public class DeleteMembershipUser : SPCITProvidersBaseWebPart
    {
        protected DropDownList loginNames;

        protected Button confirmCommand;
        protected Label result;

        protected override void CreateChildControls()
        {
            this.loginNames = new DropDownList();
            this.Controls.Add(this.loginNames);

            this.confirmCommand = new Button();
            this.confirmCommand.Click += new EventHandler(confirmCommand_Click);
            this.confirmCommand.Text = "Elimina";
            this.Controls.Add(this.confirmCommand);

            this.result = new Label();
            this.result.Text = String.Empty;
            this.Controls.Add(this.result);

            base.CreateChildControls();
        }

        protected override void OnLoad(EventArgs e)
        {
            this.EnsureChildControls();

            base.OnLoad(e);

            SPWeb webContext = SPControl.GetContextWeb(Context);

            if ((!this.Page.IsPostBack) && (webContext != null) 
                && (webContext.CurrentUser != null) 
                && (webContext.CurrentUser.IsSiteAdmin))
            {
                bindLoginNames();
            }
        }

        private void bindLoginNames()
        {
            this.loginNames.Items.Clear();
            Int32 totalRecords;
            this.loginNames.DataSource = this.GetConfiguredMembershipProvider().GetAllUsers(0, Int32.MaxValue, out totalRecords);
            this.loginNames.DataValueField = "UserName";
            this.loginNames.DataTextField = "UserName";
            this.loginNames.DataBind();
        }

        void confirmCommand_Click(object sender, EventArgs e)
        {
            SPWeb webContext = SPControl.GetContextWeb(Context);

            if ((webContext != null)
                && (webContext.CurrentUser != null)
                && (webContext.CurrentUser.IsSiteAdmin))
            {
                try
                {
                    if (this.GetConfiguredMembershipProvider() != null)
                    {
                        if (!this.GetConfiguredMembershipProvider().DeleteUser(
                            this.loginNames.SelectedValue,
                            true))
                        {
                            this.result.Text = "<BR>Cancellazione fallita.";
                        }
                        else
                        {
                            this.result.Text = "<BR>Cancellazione riuscita.";
                            bindLoginNames();
                        }
                    }
                    else
                    {
                        this.result.Text = "<BR>Membership provider non configurato.";
                    }
                }
                catch (Exception ex)
                {
                    this.result.Text = "<BR>Errore durante la cancellazione dell'utente.<BR>" + ex.Message + "<BR>" + ex.StackTrace;
                }
            }
            else
            {
                result.Text = "<BR>Utente non abilitato ad eseguire l'operazione!";
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            String loginName = string.Empty;
            SPWeb webContext = SPControl.GetContextWeb(Context);

            try
            {
                loginName = webContext.CurrentUser.LoginName;
            }
            catch (Exception)
            {
                this.result.Text += "<BR>Effettuare l'accesso prima di usare questa WebPart.";
            }

            if (loginName != String.Empty)
            {
                writer.Write("<table border=0>");

                writer.Write("<tr>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("Utente:");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.loginNames.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("<tr valign='top'>");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                this.confirmCommand.RenderControl(writer);
                writer.Write("</td>");
                writer.Write("<td class='ms-vb'>");
                writer.Write("</td>");
                writer.Write("</tr>");

                writer.Write("</table>");
                this.result.RenderControl(writer);
            }
            else
            {
                this.result.RenderControl(writer);
            }
        }
    }
}
